<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt width="40" />
        <span>电商管理后台</span>
      </div>
      <el-button type="primary" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
				<div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
					:unique-opened="true"
					:collapse="isCollapse"
					:collapse-transition="false"
					:router="true"
        >
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            
						<template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <el-menu-item :index="'/'+subItem.path" 
						v-for="subItem in item.children" :key="subItem.id"
						@click="saveNavState('/'+subItem.path)"
						>
							<template slot="title">
								<i class="el-icon-menu"></i>
								<span>{{ subItem.authName }}</span>
							</template>
						</el-menu-item>
            
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主体 -->
      <el-main>
				<router-view></router-view>
			</el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
	data(){
		return{
			menuList:[],
			iconsObj:{
				'125':'iconfont icon-user',
				'103':'iconfont icon-tijikongjian',
				'101':'iconfont icon-shangpin',
				'102':'iconfont icon-danju',
				'145':'iconfont icon-baobiao'
			},
			isCollapse:false,
			activePath:'',// 被激活的链接地址
		}
	},
	created(){
		this.getMenuList()
		this.activePath = sessionStorage.getItem('navState');
	},
  methods: {
    logout() {
      sessionStorage.clear()
      this.$router.push('/login')
		},
		toggleCollapse(){
			this.isCollapse = !this.isCollapse;
		},
		saveNavState(path){
			// 保存当前选中的状态
			sessionStorage.setItem('navState',path);
		},
		async getMenuList(){
			const {data:res} = await this.$axios.get('menus');
			if(res.meta.status!==200){
				return this.$message.error(res.meta.message)
			}
			this.menuList = res.data;
			//console.log(this.menuList,'--- menuList')
		}
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background: #373141;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 18px;
  padding-left: 0;
  & > div {
    display: flex;
    align-items: center;
    & > {
      span {
        margin-left: 15px;
      }
    }
  }
}
.el-aside {
	background: #333744;
	.toggle-button{
		background: #333994;
		color:#fff;
		text-align: center;
		line-height: 1;
		padding:5px 0;
		cursor: pointer;
		letter-spacing: 2px;
	}
	.el-menu{
		border-right:0;
	}
}
.el-main {
  background: #eaedf1;
}
.iconfont{
	margin-right:10px;
}
</style>